package com.uit.project.bustrip.main;

import java.util.ArrayList;
import java.util.List;

import com.uit.project.bustrip.R;
import com.uit.project.bustrip.activity.GenericActivity;
import com.uit.project.bustrip.bointerface.UserBO;
import com.uit.project.bustrip.entity.User;
import android.os.Bundle;
import android.util.Log;
import android.view.*;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class UserEdit extends GenericActivity<User, Integer> {

	public static final String TAG = "UserEdit Activity";
	private EditText username;
	private EditText password;
	private EditText name;
	private Button btnNew;
	private Button btnSave;
	private TextView message;
	
//	private EditText username = null;
//	private EditText password = null;
//	private Button btnSave = null;
//	private Button btnNew = null;
	private void initControl(){
		username = (EditText) findViewById(R.id.username);
		password = (EditText) findViewById(R.id.password);
		name = (EditText) findViewById(R.id.name);
		message = (TextView) findViewById(R.id.message);
		btnNew = (Button) findViewById(R.id.btnNew);
		btnSave = (Button) findViewById(R.id.btbSave);
		this.initEvent();
	}
	private void initEvent(){
		final UserEdit context = this;
		final UserBO userBO = (UserBO) this.getBo();
		btnSave.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				User user = context.getEntityData();
				long result = userBO.insert(user);
				if(result > 0){
					message.setText("Save successfully!");
				}
				else{
					message.setText("Can't save!");
				}
				List<User> userLists = userBO.findAll();
				for (int i = 0; i < userLists.size(); i++) {
					Log.i(TAG, "username " + i + " is:"+ userLists.get(i).username);
				}
			}
		});
	}
	private User getEntityData(){
		User user = new User();
		user.address = "";
		user.email = "";
		user.image = "";
		user.username = name.getText().toString();
		user.password = password.getText().toString();
		user.numberphone = "";
		user.username = username.getText().toString();
		user.status = 0;
		return user;
	}
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_user_edit);
		this.initControl();
//		ViewGroup root = (ViewGroup) getLayoutInflater().inflate(R.layout.activity_user_edit, null);
//		btnNew = (Button) findViewById(R.id.button1);
//		btnSave = (Button) findViewById(R.id.button2);
//		Integer userId = 2;
//		User u = this.getBo().findById(userId);
//		if(u != null){
//			Log.i(TAG, u.id.toString());
//			Log.i(TAG, u.address);
//			Log.i(TAG, u.email);
//			Log.i(TAG, u.image);
//			Log.i(TAG, u.name);
//			Log.i(TAG, u.password);
//		}
//		ArrayList<View> usernames = getViewsByTag(root, "username");
//		if(usernames.size()>0){
//			username = (EditText) usernames.get(0);
//		}
//		ArrayList<View> passwords = getViewsByTag(root, "password");
//		if(passwords.size()>0){
//			password = (EditText) passwords.get(0);
//		}
//		btnSave.setOnClickListener(new View.OnClickListener() {
//			
//			@Override
//			public void onClick(View arg0) {
//				// TODO Auto-generated method stub
//				Log.i(TAG, username.getText().toString());
//				Log.i(TAG, password.getText().toString());
//				String ut = username.getText().toString();
//				String pt = password.getText().toString();
//			}
//		});
//		UserBO bo = (UserBO) this.getBo();
//		User user = new User();
//		List<User> list = bo.findAll();
//		for (User u1 : list) {
//			
//		}
//		
	}
	private static ArrayList<View> getViewsByTag(ViewGroup root, String tag){
	    ArrayList<View> views = new ArrayList<View>();
	    final int childCount = root.getChildCount();
	    for (int i = 0; i < childCount; i++) {
	        final View child = root.getChildAt(i);
	        if (child instanceof ViewGroup) {
	            views.addAll(getViewsByTag((ViewGroup) child, tag));
	        }

	        final Object tagObj = child.getTag();
	        if (tagObj != null && tagObj.equals(tag)) {
	            views.add(child);
	        }

	    }
	    return views;
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.user_edit, menu);
		return true;
	}

	
}
